package com.youdianda.controller.admin;


import com.youdianda.constant.JwtClaimsConstant;
import com.youdianda.dto.AdminLoginDTO;
import com.youdianda.entity.SysUser;
import com.youdianda.properties.JwtProperties;
import com.youdianda.result.Result;
import com.youdianda.service.IndexService;
import com.youdianda.util.JwtUtil;
import com.youdianda.vo.BaseInfoVO;
import com.youdianda.vo.GetConfigVO;
import com.youdianda.vo.ProfileVO;
import com.youdianda.vo.SysUserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.login.AccountLockedException;
import javax.security.auth.login.AccountNotFoundException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/index")
@Slf4j
@Api(tags = "index接口")
public class IndexController {
    @Autowired
    private IndexService indexService;
    @Autowired
    private JwtProperties jwtProperties;


    /**
     * GET 获取系统配置
     *
     * @return
     */
    @GetMapping("/getConfig")
    @ApiOperation("获取系统配置")
    public Result<GetConfigVO> getConfig() {
        GetConfigVO getConfigVO=indexService.getConfig();
        return Result.success(getConfigVO);
    }

    /**
     * 主页信息统计
     *
     * @return
     */
    @GetMapping("/baseInfo")
    @ApiOperation("主页统计信息")
    public Result baseInfo() {
        BaseInfoVO baseInfoVO = indexService.baseInfo();
        System.out.println(baseInfoVO);
        return Result.success(baseInfoVO);
    }

    /**
     * 登录接口
     *
     * @param adminLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("后台登陆")
    public Result login(@RequestBody AdminLoginDTO adminLoginDTO) throws AccountLockedException, AccountNotFoundException {
        log.info("后台登录：{}", adminLoginDTO);

        SysUser sysUser = indexService.login(adminLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        //JwtClaimsConstant.SYS_USER_ID--empId通用常量
        claims.put(JwtClaimsConstant.SYS_USER_ID, sysUser.getId());
        claims.put(JwtClaimsConstant.SYS_USER_LOGIN_NAME, sysUser.getLoginName());
        //调用工具类JwtUtil的createJWT方法，传入三个参数
        String token = JwtUtil.createJWT(
                //设置jwt签名加密时使用的秘钥
                jwtProperties.getAdminSecretKey(),
                //设置jwt过期时间
                jwtProperties.getAdminTtl(),
                //设置前端传递过来的令牌名称,自定义载荷
                claims);
        SysUserLoginVO sysUserLoginVO = SysUserLoginVO.builder()
                /*.id(sysUser.getId())
                .loginName(sysUser.getLoginName())
                .name(sysUser.getName())*/
                .token(token)
                .build();
        return Result.success(sysUserLoginVO);
    }

    /**
     * 获取用户资料
     *
     * @return
     */
    @PostMapping("/profile")
    @ApiOperation("获取用户资料")
    public Result profile() {
        ProfileVO profileVO = indexService.profile();
        return Result.success(profileVO);
    }
}
